<template>
  <div>
    <!--  填写国内运单号弹窗  -->
    <el-dialog
      title="填写国内运单信息"
      :visible.sync="isOpenDomesticWaybill"
      width="500px"
      :before-close="closeDomesticWaybill">
      <el-form size="small" :model="domesticWaybillInfo" :rules="domesticWaybillInfoRules" ref="domesticWaybillRef"
               label-width="120px">
        <el-form-item label="运单号：" prop="trackingNo">
          <el-input v-model="domesticWaybillInfo.trackingNo" placeholder="请输入运单号"/>
        </el-form-item>
        <el-form-item label="预计到货日期：" prop="arrivalTime">
          <el-date-picker
            v-model="domesticWaybillInfo.arrivalTime"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            default-time="23:59:59"
            placeholder="选择预计到货日期"
            @change="changeTime"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发货类型：" prop="type">
          <el-select v-model="domesticWaybillInfo.type" placeholder="请选择发货类型" size="small">
            <el-option v-for="(item,index) in sendOutTypeList" :key="index" :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称：" prop="kjxProductName" v-if="domesticWaybillInfo.type=='3'">
          <el-input v-model="domesticWaybillInfo.kjxProductName" placeholder="请输入商品名称"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDomesticWaybill()">取 消</el-button>
        <el-button type="primary" @click="confirmWaybillNo()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  confirmDomesticWaybill, querySendGoodsInfo, uploadProof,
} from "@/api/order/purchaser"
import {formatDate} from "@/utils";

export default {
  name: 'order-list',
  props: {
    //子订单商品规格编码
    skuCode: {
      type: String / Number
    },
    // 发货类型
    sendOutTypeList: {
      type: Array,
      default: []
    },
    // 子订单号
    childOrderId: {
      type: String / Number
    },
    // 订单号
    orderId: {
      type: String / Number
    },
    isCutOrder: {
      type: Boolean,
    }
  },
  data() {
    return {
      isOpenDomesticWaybill: true,
      domesticWaybillInfo: {},//国内运单号信息
      arrivalTime: "",//到达时间
      domesticWaybillInfoRules: { // 线上发货表单校验
        trackingNo: [{required: true, message: '国内运单号不能为空', trigger: 'blur'}],
        arrivalTime: [{required: true, message: '预计到达时间不能为空', trigger: 'blur'}],
        type: [{required: true, message: '请选择发货类型', trigger: 'change'}],
        kjxProductName: [{required: false, message: '商品名称不能为空', trigger: 'blur'}]
      },
    }
  },
  mounted() {
    this.getSendGoodsInfo()
  },
  methods: {
    // 确定国内运单号
    confirmWaybillNo() {
      this.$refs['domesticWaybillRef'].validate((valid) => {
        if (valid) {
          const time = this.domesticWaybillInfo.arrivalTime.replace('00:00:00', '23:59:59')
          // console.log('time', time)
          this.arrivalTime = new Date(time).getTime()
          // console.log('this.arrivalTime', this.arrivalTime)
          let data = {
            orderId: this.domesticWaybillInfo.orderId,
            childOrderId: this.domesticWaybillInfo.childOrderId,
            arrivalTime: this.arrivalTime,
            type: this.domesticWaybillInfo.type,
            trackingNo: this.domesticWaybillInfo.trackingNo,
            kjxProductName: this.domesticWaybillInfo.kjxProductName
          }
          // console.log('data', data)
          // return;
          confirmDomesticWaybill(data).then(res => {
            this.$message.success(res.msg)
            this.closeDomesticWaybill('1')
          })
        }
      })
    },
    // 选择时间
    changeTime(e) {
      console.log('ee,选择时间', e)
      // const time = e.replace('00:00:00', '23:59:59')
      // console.log('time', time)
      // this.arrivalTime = new Date(time).getTime()
      // console.log('this.arrivalTime', this.arrivalTime)
    },
    // 获取已上传的凭证和运单号信息
    getSendGoodsInfo() {
      this.purchaseVoucherInfo = {}
      let data = {
        childOrderId: this.childOrderId
      }
      querySendGoodsInfo(data).then(res => {
        let data = res.data
        if (data) {
          this.domesticWaybillInfo = data
          this.domesticWaybillInfo.arrivalTime = formatDate(data.arrivalTime)
          this.domesticWaybillInfo.type = data.type.toString()
          if (this.domesticWaybillInfo.type == '0') {
            this.domesticWaybillInfo.type = undefined
          }
        }
      })
    },
    // 关闭线上发货弹窗
    closeDomesticWaybill(e) {
      this.$emit('domesticWaybillSwitch', e)
    },
  }
}
</script>
<style scoped>
.el-divider__text {
  font-size: 16px;
  font-weight: bold;
  color: #000;
}
</style>
